package com.yunji.base.business;

import com.alibaba.druid.sql.visitor.functions.Char;
import com.yunji.base.dto.UpgradeFileDTO;
import com.yunji.base.engine.calculate.FileSizeCalculate;
import com.yunji.base.vo.UpgradeFileVO;
import com.yunji.cabinet.domain.UpgradeFileDO;
import com.yunji.cabinet.params.CabinetVersionParam;
import com.yunji.cabinet.service.IUpgradeFileService;
import com.yunji.common.enums.common.ErrorEnum;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.common.utils.file.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.List;

@Slf4j
@Component
public class UpgradeFileBusiness {


    @Resource
    private IUpgradeFileService upgradeFileService;


    @Value("${yunji.environment}")
    private String environment;

    private final static String PROJECT_CODE = "SZJ";

    /**
     * 存入版本文件
     *
     * @param dto
     * @Date 2024-07-01 10:12:12.465
     */
    public Long cabinetVersionFile(UpgradeFileDTO dto) {

        log.info("【固件升级文件】上传文件名:{}", dto.getFileName());
        Assert.isTrue(!upgradeFileService.checkUniqueName(dto.getFileId(), dto.getFileName()), ErrorEnum.BUSINESS_712003.getText());

        Assert.isTrue(PROJECT_CODE.equals(dto.getProjectCode()), ErrorEnum.BUSINESS_712009.getText());
        Assert.isTrue(environment.equals(dto.getEnvironment()), ErrorEnum.BUSINESS_712010.getText());

        UpgradeFileDO param = new UpgradeFileDO();
        BeanUtils.clone(dto,param);
        try {
            param.setPack(dto.getFile().getBytes());
        } catch (IOException e) {
            return null;
        }
        param.setPackSize(FileSizeCalculate.getFileSize(dto.getFile().getSize()));
        Assert.isTrue(upgradeFileService.create(param), "新增失败");
        return param.getFileId();

    }


    /**
     * 获取版本列表
     *
     * @param param
     * @return
     */
    public List<UpgradeFileVO> search(CabinetVersionParam param) {
        return upgradeFileService.searchVO(param);
    }

    public UpgradeFileVO getInfo(Long fileId) {
        return upgradeFileService.selectVO(fileId);
    }

    @Transactional
    public boolean update(UpgradeFileDTO dto) {
        Long fileId = dto.getFileId();
        Assert.isTrue(!upgradeFileService.checkUniqueName(fileId, dto.getFileName()), ErrorEnum.BUSINESS_712003.getText());

        UpgradeFileDO bo = new UpgradeFileDO();
        BeanUtils.clone(dto,bo);

        //将版本信息存入数据库;
        bo.setPackSize(FileSizeCalculate.getFileSize(dto.getFile().getSize()));
        return upgradeFileService.update(bo);
    }

    public boolean delete(Long fileId) {
        return upgradeFileService.delete(fileId);
    }

    public byte[] download(Long fileId) {
        byte[] bytes = upgradeFileService.selectPack(fileId);
        File file = null;
        try {
            file = File.createTempFile("temp",".bin");
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(bytes);
            fos.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        try (RandomAccessFile access = new RandomAccessFile(file, "r")) {
            long len = access.length();
            log.info("长度:{}",len);
        } catch (IOException e) {
            return null;
        }
        return bytes;
    }
}
